package cn.yanhu.kuwanapp.my

import android.view.View
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import cn.woochen.comlib.BaseFragment
import cn.woochen.comlib.adapter.OnItemClickListener
import cn.woochen.comlib.util.OnSingleClickListener
import cn.woochen.comlib.util.ext.setOnSingleClickListener
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.bean.response.RespMyConfigBean
import cn.yanhu.kuwanapp.databinding.FragmentMyBinding
import cn.yanhu.kuwanapp.my.activity.PersonInfoActivity
import cn.yanhu.kuwanapp.my.activity.WithdrawActivity
import cn.yanhu.kuwanapp.my.activity.WithdrawRecordActivity
import cn.yanhu.kuwanapp.my.adapter.MyAdAdapter
import cn.yanhu.kuwanapp.my.adapter.MyBottomMenuAdapter
import cn.yanhu.kuwanapp.my.adapter.MyTopMenuAdapter
import cn.yanhu.kuwanapp.user.UserViewModel
import cn.yanhu.kuwanapp.utils.NavigatorHelper
import cn.yanhu.kuwanapp.utils.ext.logcom
import cn.yanhu.kuwanapp.utils.ext.parseState

/**
 *
 * 类描述：我的
 * @author：woochen
 * create at：2021/7/27 10:38 PM
 */
class MyFragment : BaseFragment<FragmentMyBinding, UserViewModel>(), OnSingleClickListener {

    private val mMyTopMenuDatas by lazy {
        mutableListOf<RespMyConfigBean.Icon>()
    }
    private val mMyTopMenuAdapter by lazy {
        MyTopMenuAdapter(mMyTopMenuDatas)
    }


    private val mMyBottomMenuDatas by lazy {
        mutableListOf<RespMyConfigBean.Config>()
    }
    private val mMyBottomMenuAdapter by lazy {
        MyBottomMenuAdapter(mMyBottomMenuDatas)
    }

    private val mMyAdDatas by lazy {
        mutableListOf<RespMyConfigBean.Banner>()
    }

    private val mMyAdAdapter by lazy {
        MyAdAdapter(mMyAdDatas)
    }

    override fun setContentView(): Int = R.layout.fragment_my

    override fun initData() {
        initWidgets()
    }

    override fun initListener() {
        mBinding.cllPersonInfo.setOnSingleClickListener(this)
        mBinding.tvWithdraw.setOnSingleClickListener(this)
        mBinding.tvTopTitle.setOnSingleClickListener(this)
    }

    private fun initWidgets() {
        //top menu
        mBinding.rvTopMenu.adapter = mMyTopMenuAdapter
        mMyTopMenuAdapter.mItemClickListener = OnItemClickListener {
            val data = mMyTopMenuDatas[it]
            NavigatorHelper.toPage(mContext,data.url)
        }
        //banner
        mBinding.banner.addBannerLifecycleObserver(this).setUserInputEnabled(true).setAdapter(mMyAdAdapter)
            .setOnBannerListener { data, _ ->
                logcom(data.toString())
                if (data is RespMyConfigBean.Banner) {
                    NavigatorHelper.toPage(mContext,data.linkUrl)
                }
            }.start()
        //bottom menu
        mBinding.rvBottomMenu.adapter =  mMyBottomMenuAdapter
        mMyBottomMenuAdapter.mItemClickListener = OnItemClickListener {
            val data = mMyBottomMenuDatas[it]
            NavigatorHelper.toPage(mContext,data.url)
        }
    }

    override fun onStart() {
        super.onStart()
        if (!isHidden) requestData()
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        //我的页面
        mViewModel.userInfoInfo.observe(this, Observer { it ->
            parseState(it, {
                mBinding.userInfo = it
            })
        })
        //我的页面配置
        mViewModel.myConfigInfo.observe(this, Observer { it ->
            parseState(it, {
                //top menu
                mMyTopMenuDatas.clear()
                mMyTopMenuDatas.addAll(it.icons)
                mMyTopMenuAdapter.notifyDataSetChanged()
                //banner
                mBinding.banner.isVisible = !it.banners.isNullOrEmpty()
                mMyAdDatas.clear()
                mMyAdDatas.addAll(it.banners)
                mMyAdAdapter.notifyDataSetChanged()
                //bottom menu
                mMyBottomMenuDatas.clear()
                mMyBottomMenuDatas.addAll(it.configs)
                mMyBottomMenuAdapter.notifyDataSetChanged()
            })
        })
    }

    override fun requestData() {
        mViewModel.userInfo("1")
        mViewModel.myConfig()
    }

    fun refreshData() {
        requestData()
    }

    companion object {
        fun newInstance() = MyFragment()
    }

    override fun onSingleClick(v: View?) {
        when (v) {
            mBinding.cllPersonInfo -> {//个人中心
                PersonInfoActivity.start(mContext)
            }
            mBinding.tvWithdraw -> {//提现
                WithdrawActivity.start(mContext)
            }
            mBinding.tvTopTitle -> {//提现记录
                WithdrawRecordActivity.start(mContext)
            }
        }
    }
}